import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { User } from '../../auth/entities/user.entity';

/**
 * 好友关系状态
 */
export enum FriendshipStatus {
  PENDING = 'pending', // 待接受
  ACCEPTED = 'accepted', // 已接受
  REJECTED = 'rejected', // 已拒绝
  BLOCKED = 'blocked', // 已拉黑
}

/**
 * 好友关系实体
 */
@Entity('friendships')
@Index(['userId', 'friendId'], { unique: true }) // 防止重复添加
export class Friendship {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ comment: '发起者ID' })
  @Index()
  userId: number;

  @Column({ comment: '好友ID' })
  @Index()
  friendId: number;

  @Column({
    type: 'enum',
    enum: FriendshipStatus,
    default: FriendshipStatus.PENDING,
    comment: '好友状态',
  })
  status: FriendshipStatus;

  @Column({ type: 'text', nullable: true, comment: '申请备注' })
  remark: string | null;

  @ManyToOne(() => User, { eager: true, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'userId' })
  user: User;

  @ManyToOne(() => User, { eager: true, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'friendId' })
  friend: User;

  @CreateDateColumn({ type: 'timestamp', comment: '创建时间' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', comment: '更新时间' })
  updatedAt: Date;
}
